package com.lijing.binarysearch;

/**
 * @Description TODO
 * @title: BinarySearchNoRecurion
 * @Author LiJing
 * @Date: 2021/4/11:08 上午
 * @Version 1.0
 */
public class BinarySearchNoRecursion {
    public static void main(String[] args) {
        int[] arr = {1,3,4};
        System.out.println(binarySearch(arr,4));
    }

    public static int binarySearch(int[] arr, int i){
        int low = 0;
        int high = arr.length-1;
        int mid;
        while (low <= high){
            mid = (low + high)/2;
            if (arr[mid] == i){
                return mid;
            }else if (arr[mid] < i){
                low = mid + 1;
            }else {
                high = mid - 1;
            }
        }
        return -1;
    }
}
